
class ListNode:
    def __init__(self,val):
        self.val = val
        self.next = None


def reverseList(head):
    cur, pre = head, None
    while cur:
        tmp = cur.next
        cur.next = pre
        pre = cur
        cur = tmp
    return pre  #返回链表头结点

# 构造链表
l = ListNode(1)
l.next = ListNode(2)
l.next.next = ListNode(3)
l.next.next.next = ListNode(4)
l.next.next.next.next = ListNode(5)

l=reverseList(l)
cur=l

# 遍历
while cur.next != None:
    print(cur.val)
    cur = cur.next
print(cur.val)
